package cn.b.sky.mes.pp.controller;

import cn.b.sky.common.file.ExcelUtil;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.utils.DateUtils;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.mes.pp.model.OrderType;
import cn.b.sky.mes.pp.model.ProductionControl;
import cn.b.sky.mes.pp.model.ProductionOrder;
import cn.b.sky.mes.pp.model.condition.ProductionOutputCondition;
import cn.b.sky.mes.pp.service.OrderTypeService;
import cn.b.sky.mes.pp.service.ProductionConfirmDetailService;
import cn.b.sky.mes.pp.service.ProductionControlService;
import cn.b.sky.mes.pp.service.ProductionOutputService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * <strong>Title : ProductOutputController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.pp.controller<br>
 * </strong> <strong>Description : </strong>产量状态分析控制器<br>
 * <strong>Create on : 2015-10-14 下午4:26:29<br>
 * </strong>
 * <p>
 * 
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/productionOutput/*")
@Slf4j
public class ProductionOutputController extends SecurityController {

	private static final String LIST_PAGE = "pp/productionOutput/productionOutputList";

	@Autowired
	private ProductionConfirmDetailService productionConfirmDetailService;

	@Autowired
	private OrderTypeService orderTypeService;

	@Autowired
	ECodeService eCodeService;

	@Autowired
	ProductionOutputService productionOutputService;

	@Autowired
	ProductionControlService productionControlService;

	// 生产订单
	@RequestMapping("productionOutputList")
	public String productionOutputList(Model model, String navTabId) {
		bindModelAttributes(model, navTabId);
		return LIST_PAGE;
	}

	// 产品订单
	@RequestMapping("productOutputList")
	public String productOutputList(Model model, String navTabId) {
		// 订单类型
		List<OrderType> orderTypes = orderTypeService.getAllOrderTypes(this
				.getSiteCode());
		model.addAttribute("orderTypes", orderTypes);
		model.addAttribute(NAVTABID, navTabId);
		return "pp/productOutput/product_output";
	}

	private void bindModelAttributes(Model model, String navTabId) {
		// 订单类型
		List<OrderType> orderTypes = orderTypeService.getAllOrderTypes(this
				.getSiteCode());
		model.addAttribute("orderTypes", JSON.toJSONString(orderTypes));
		// 订单状态
		model.addAttribute("ecodes", JSON.toJSONString(eCodeService
				.selectECodeList(ECodeEnums.PRODUCTIONORDER_TYPE.getCode(),
						SecurityContextUtil.getLanguageCode())));
		// 生产管理状态
		model.addAttribute("controlEcodes", JSON.toJSONString(eCodeService
				.selectECodeList(ECodeEnums.PRODUCTIONCONTROL_STATUS.getCode(),
						SecurityContextUtil.getLanguageCode())));
		model.addAttribute(NAVTABID, navTabId);
	}

	@RequestMapping("getProductionOutputList")
	@ResponseBody
	public DWZResult getProductionOutputList(ProductionOutputCondition condition) {
		condition.setSiteCode(this.getSiteCode());
		condition.setToDay(DateUtils.getDateUtils().getSystemStrDate());
		condition.setData(productionOutputService
				.selectProductionsByCondition(condition));
		condition.setTotalCount(productionOutputService
				.selectProductionsCountByCondition(condition));
		return ajaxDone(condition);
	}

	@RequestMapping("viewProductionControls")
	public String viewProductionOrderDetail(Model model,
			String productionOrderNum, String navTabId) {
		bindModelAttributes(model, null);
		model.addAttribute("productionOrderNum", productionOrderNum);
		return "pp/productionOutput/productionOutputView";
	}

	@RequestMapping(value = "getProductionConfirmRecordes")
	@ResponseBody
	public DWZResult getProductionConfirmRecordes(String productionControlNum) {
		return ajaxDone(productionConfirmDetailService
				.getConfirmDetailsByControlNum(getSiteCode(),
						productionControlNum));
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportProductionOrdersToExcel")
	public void exportProductionOrdersToExcel(HttpServletResponse response,
			ProductionOutputCondition condition, String colsMap,
			String outputStatusMap) {
		condition.setSiteCode(this.getSiteCode());
		condition.setToDay(DateUtils.getDateUtils().getSystemStrDate());
		List<ProductionOrder> orders = productionOutputService
				.selectProductionsByCondition(condition);
		setOrderStatusText(orders, JSON.parseObject(outputStatusMap, Map.class));
		String title = condition.isProductOrder() ? getImeViewMessageI18n(
				SecurityContextUtil.getLanguageCode(), "productOutput.C")
				: getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
						"productionOutput.C");
		try {
			ExcelUtil.exportToExcel(response, title,
					ExcelUtil.parseColumnsStr2Map(colsMap), orders);
		} catch (IOException e) {
			log.error("{}", e);
		}
	}

	@RequestMapping(value = "exportConfirmRecordesToExcel")
	public void exportConfirmRecordesToExcel(HttpServletResponse response,
			String productionControlNum, String colsMap) {
		try {
			ExcelUtil.exportToExcel(
					response,
					getImeViewMessageI18n(
							SecurityContextUtil.getLanguageCode(),
							"productionConfirmRecordes.C")
							+ productionControlNum, ExcelUtil
							.parseColumnsStr2Map(colsMap),
					productionConfirmDetailService
							.getConfirmDetailsByControlNum(getSiteCode(),
									productionControlNum));
		} catch (IOException e) {
			log.error("{}", e);
		}
	}

	private void setOrderStatusText(List<ProductionOrder> orders,
			Map<String, String> outputStatusMap) {
		Map<String, String> statusMap = transEcodes2Map(eCodeService
				.selectECodeList(ECodeEnums.PRODUCTIONORDER_TYPE.getCode(),
						SecurityContextUtil.getLanguageCode()));
		for (ProductionOrder order : orders) {
			order.setStatuseText(statusMap.get(order.getStatuseCode()));
			order.setOutputStatusText(outputStatusMap.get(order
					.getOutputStatus().toString()));
		}
	}

	private void setControlStatusText(List<ProductionControl> controls) {
		Map<String, String> statusMap = transEcodes2Map(eCodeService
				.selectECodeList(ECodeEnums.PRODUCTIONCONTROL_STATUS.getCode(),
						SecurityContextUtil.getLanguageCode()));
		for (ProductionControl control : controls) {
			control.setStatuseText(statusMap.get(control.getStatuseCode()));
		}
	}

	private Map<String, String> transEcodes2Map(List<ECode> eCodes) {
		Map<String, String> map = new HashMap<String, String>();
		for (ECode eCode : eCodes) {
			map.put(eCode.geteCode(), eCode.geteCodeText());
		}
		return map;
	}
}
